package org.core.blog.service.impl;

import org.core.blog.mapper.LoginMapper;
import org.core.blog.pojo.LoginInfo;
import org.core.blog.pojo.User;
import org.core.blog.service.LoginService;
import org.core.blog.utils.JwtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    private static final Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);
    @Autowired
    private LoginMapper loginMapper;

    @Override
    public LoginInfo login(User user) {
        User u = loginMapper.selectByUsernameAndPassword(user);
        if (u != null) {
            //  生成JWT令牌
            Map<String,Object> claims = new HashMap<>();
            claims.put("id", u.getId());
            claims.put("username", u.getUsername());
            String token = JwtUtils.generateJwt(claims);
            LoginInfo info = new LoginInfo(u.getId(), u.getUsername(), token);
            return info;
        }
        return null;
    }
}
